import {
    createRouter,
    createWebHashHistory
} from 'vue-router'
import store from '@/store'
import { ElNotification } from 'element-plus';//这个要添加
import { extractNames } from '@/utils/transform';


import layout from '@/layout'
import permissions from './modules/permissions'
import OA from './modules/OA';
import declare from './modules/declare';
import coefficient from './modules/coefficient';
import back from './modules/back';
import performSet from './modules/performSet';
import EmpHome from './modules/empHome'
import result from './modules/result';
import person from './modules/person';
import report from "./modules/report"

/**
 * 私有路由表
 */
export var privateRoutes = [
    EmpHome,
    permissions,
    performSet,
    declare,
    OA,
    coefficient,
    back,
    result,
    person,
	report
]
/**
 * 公开路由表
 */
export var publicRoutes = [
    {
        path: '/login',
        name: 'login',
        component: () => import('@/views/login/index')
    },
    {
        path: '/',
        // 注意：带有路径“/”的记录中的组件“默认”是一个不返回 Promise 的函数
        component: layout,
        //redirect: '/home',
        children: [
            /*{
                path: '/home',
                name: 'home',
                component: () => import('@/views/home/index'),
                meta: {title: '首页', icon:'home' , affix: true},//affix=true,tagViews右侧没有关闭按钮
                hidden: true,//true不显示在侧边栏
            },*/
            {
                path: '/404',
                name: '404',
                component: () => import('@/views/error-page/404')
            },
            {
                path: '/401',
                name: '401',
                component: () => import('@/views/error-page/401')
            },
            {
                path: '/emp/accredit',
                name: 'empAccredit',
                component: () =>
                    import(
                        '@/views/permissions-page/role-management/components/accredit/index.vue'
                    ),
                meta: { title: '授权用户', icon: 'personnel' },
                hidden: true, //true不显示在侧边栏
            },
            {
                path: '/emp/detail',
                name: 'empDetail',
                component: () =>
                    import(
                        '@/views/permissions-page/accountDetail.vue'
                    ),
                meta: { title: '个人信息', icon: 'personnel' },
                hidden: true, //true不显示在侧边栏
            },
            {
                path: '/emp/error',
                name: 'empError',
                component: () =>
                    import(
                        '@/views/permissions-page/user-management/components/showError.vue'
                    ),
                meta: { title: '导入错误', icon: 'personnel' },
                hidden: true, //true不显示在侧边栏
            },
        ]
    },
    //测试页面
    // {
    //     path: '/test',
    //     component: () => import('@/views/test-page/test'),
    //
    // },
]


/**
 * 初始化路由表
 */
export function resetRouter() {
    if (store.getters.hasRoles) {
        //公共路由name
        const publicR = extractNames(publicRoutes) || [];
        const publicSet = new Set(publicR);
        const allRoutes = extractNames(router.getRoutes()) || [];
        //要删除的路由name
        const removeNames = allRoutes.filter((name) => !publicSet.has(name));

        // const menus = store.getters.roles 
        //removeRoute是根据路由的name去删除路由的，所以我们要对路由的名字进行截取
        // const menus = ['getRoleList','admintorList','adminAuth']
        // console.log("menus==",menus)
        // console.log("router==",router.getRoutes())
        // menus.forEach(menu => {
        //     let url = menu.url
        //     let i = url.lastIndexOf('/')
        //     let name = url.substring(i + 1, url.length)
        //     router.removeRoute(name)
        // })
        removeNames.forEach(name => {
            router.removeRoute(name)
        })
    }

}

const router = createRouter({
    history: createWebHashHistory(),
    // routes: [...publicRoutes, ...privateRoutes]
    routes: publicRoutes

})

// 在路由跳转之前执行的导航守卫
router.beforeEach((to, from, next) => {
    // 在这里执行路由跳转前的操作
    next();
});

// 在路由跳转完成后执行的导航守卫
/*router.afterEach((to, from) => {
  // 判断是否跳转到了 '/home' 页面
  if (to.path === '/home' && from.path === '/login') {
    // 显示通知消息
    ElNotification({
      title: '登录成功',
      message: `欢迎使用绩效考核系统，${store.getters.userInfo.name}！`,
      type: 'success',
      offset: 40
    });
  }
});*/
// 在路由跳转完成后执行的导航守卫
router.afterEach((to, from) => {
    // 判断是否跳转到了 '/home' 页面
    if (from.path === '/login' && to.path !== '/login') {
        // 显示通知消息
        ElNotification({
            title: '登录成功',
            message: `欢迎使用绩效考核系统，${store.getters.userInfo.name}！`,
            type: 'success',
            offset: 40
        });

        // 员工没有修改默认密码则通知修改默认密码
        if (store.getters.userInfo.isChangePwd === 0) {
            ElNotification({
                title: '请尽快修改密码！',
                type: 'warning',
                offset: 130,
                duration: 0
            });
        }
    }
});


export default router


